<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        function myAxios(config) {
            return new Promise((resovle, reject) => {
                const { method, url, data, params } = config
                const xhr = new XMLHttpRequest()
                if (method.toUpperCase() === "GET") {
                    let str = ''
                    for (let key in params) {
                        if (Object.hasOwnProperty.call(params, key)) {
                            str += key + '=' + params[key] + '&'
                        }
                    }
                    console.log(str)
                    xhr.open(method, params ? url + '?' + str : url)
                    xhr.send()
                } else {
                    let flag = Object.prototype.toString.call(data) === '[object Object]'
                    xhr.open('post', url)
                    xhr.setRequestHeader('Content-Type', flag ? 'application/json' : 'application/x-www-form-urlencoded')
                    xhr.send(flag ? JSON.stringify(data) : data)

                }
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status >= 200 && xhr.status < 300) {
                            console.log(xhr.responseText)
                            resovle(JSON.parse(xhr.responseText))
                        }
                    }
                }
            })

        }
    </script>
    <script>
        myAxios({
            method: 'post',
            url: 'http://ajax-api.itheima.net/api/login',
            data: {
                username: 'admin',
                password: '123456'
            }
        }).then((res) => {
            console.log(res)
        })
    </script>
</body>

</html>